<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    
 *
 * 预存款  2017-03-11 
 * 
 */
namespace osc\mobile\controller;
use osc\common\controller\Base;
use osc\mobile\model\Member;
use \think\Db;
use think\Request;
class MyTicket extends Base
{
	/**
	 * 登陆验证
	 * @return int
	 */
	function check_login(){
		
		$uid=is_login();
		
		if(!$uid){			
			$this->redirect('login/login');					
		}

		$username = session('username');
		
		if(!$username)
		{
			$this->redirect('login/login');					
		}
		return $uid;
	}

	/**
	 * 票兑换
	 * @return mixed
	 */
	public function index(){
		
		$uid=$this->check_login();
		$username = session('username');
		if(!$username)
		{
			$this->redirect('login/login');					
		}

		$start = input('param.start');
		$end = input('param.end');
		if($start && $end){
			$map['create_time'] = array(['>',strtotime($start)],['<',strtotime($end)]);
		}
		$map['username'] = $username;
		$list = Db::name('raise_ticket')
				->where($map)
				->order('state asc,create_time desc')
				->paginate(5,false, [
						'query' => Request::instance()->param(),//不丢失已存在的url参数
				]);

//		print_r($list);exit;

		$page=$list->render(); //获取分页

		$user_info=Db::name('member')->where(array('uid'=>$uid))->find();
		$this->assign('user_info',$user_info);
		$this->assign('start',$start);
		$this->assign('end',$end);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('empty',"<span style='margin-left:20px;'>没有数据</span>");
				
		$this->assign('top_title','发行认购');
		$this->assign('SEO',['title'=>'发行认购-'.config('SITE_TITLE')]);
				
		return $this->fetch();
	}



	/**
	 * 票转让验证
	 * @return array
	 */
	function getUserinfo()
	{
		if(request()->isPost()){

			$data=input('post.');

			$username = session('username');

			$pwd2 = think_ucenter_encrypt($data['pwd2'],config('PWD_KEY'));

			$user=Db::name('member')->where('username',$username)->find();

			if($username == $data['mobile']){ return array('error'=>'不能转给自己'); }

			if($data['num'] > $user['ticket']){ return array('error'=> '可用票数不够'); }


			if(ceil($data['num']/0.5) != ($data['num']/0.5)){

				return array('error'=> '当前票数不是0.5的倍数');
			}

			if($pwd2!=$user['2password'])
			{

				return array('error'=>'二级密码输入有误');
			}

			if($data['mobile'])
			{
				$ret = Db::name('member')->where('username',$data['mobile'])->find();
				if(!$ret){
					return array('error'=>'用户不存在');
				}
				return array('success'=>$ret['idcardname']?$ret['idcardname']:'未认证用户');
			}
		}
	}

    //票转让操作
	function usertf()
	{
		$user = session('username');

		if(request()->isPost()){

			$data=input('post.');
			$map = [
				'token'			=> $data['__token__'],
				'username'		=> $user,
				'mobile'		=> $data['mobile'],
				'num'			=> $data['num'],
				'id_name'		=> get_username_card_name($data['mobile']),
			];
			Db::startTrans();
			try {
				$rew = Db::name('user_ticket')->insert($map);

				$member = new Member();
				$res = $member->where('username', $user)->setDec('ticket', $data['num']);
				$red = $member->where('username', $data['mobile'])->setInc('ticket', $data['num']);
				/*print_r($rew);
				print_r($red);
				print_r($res);exit;*/
				Db::commit();
				return array('success' => '转让成功');

			}catch (\Exception $e){
				Db::rollback();
				return array('error'=>$e->getMessage());
			}
		}
		$list = Db::name('user_ticket')->where(array('username'=>$user))->order('dt desc')->limit(50)->select();
		$this->assign('list',$list);
		$this->assign('top_title','交易品转让');
		$this->assign('SEO',array('title'=>'交易品转让-'.config('SITE_TITLE')));
		return $this->fetch();

	}

	/**
	 * 票兑换
	 * @return array|mixed
	 */
	public function wd()
	{
		$username = session('username');

		if(request()->isPost()){
			$data=input('post.');
			
			/*if($data['variety_id']=="")
			{
				return array('error'=>'请选择交易品种');
			}*/

//			return array('error'=>'暂未开放');

			if($data['num']=="")
			{
				return array('error'=>'请输入转换数量');
			}

			if(!(floor($data['num'])==$data['num']))
			{
				return array('error'=>'请输入正确的数量!');
			}

			if($data['num']<1)
			{
				return array('error'=>'请输入正确的数量');
			}

			$pwd2 = think_ucenter_encrypt($data['pwd2'],config('PWD_KEY'));

			$user=Db::name('member')->where('username',$username)->find();

			if($pwd2!=$user['2password'])
			{

				return array('error'=>'二级密码输入有误');
			}

			if($data['num'] > $user['ticket']){ return array('error'=> '可用票数不够'); }


			if(ceil($data['num']) != $data['num']){

				return array('error'=> '请输入整数');
			}

			$zong = $data['num']*150;

			$map = [
				'token'		=> $data['token'],
				'username'	=> $username,
				'type'		=> 1,
				'num'			=> $data['num'],
				'money'			=> $zong,
				'description'			=> '转换宝豆吉豆',
				'create_time'		=> time(),
			];


			// 启动事务
			Db::startTrans();
			try{

				Db::name('raise_ticket')->insert($map);

				$member = new Member();

				//减少用户票数
				$member->where('username',$username)->setDec('ticket',$map['num']);

				$bao   = round(($zong*0.8)*5,2);
				$jilt  = round($zong*0.2,2);

				Db::name('member')->where('username',$username)->setInc('bao',$bao);
				Db::name('member')->where('username',$username)->setInc('jilt',$jilt);


				// 更新成功 提交事务
				Db::commit();

				return array('success'=>'申请兑换成功','url'=>'/mobile');

			} catch (\Exception $e) {
				// 更新失败 回滚事务
				Db::rollback();
				return array('error'=>'申请失败','url'=>'/mobile');
			}

		}

		$exchange = Db::name('exchange_house')->where('id',3)->select();
		$variety = Db::name('exchage_variety')->where('id',5)->select();
		$this->assign('exchange',$exchange);
		$this->assign('variety',$variety);
		$this->assign('top_title','交易品兑换');
		return $this->fetch();
	}

}